using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spinning.Spinning.Interface;
using Spinning.Spinning.Unitity;
using Microsoft.Xna.Framework;

namespace Spinning.Spinning.Entity.Particle
{
    class ParticleActor : Actor, IParticle
    {
        public delegate void OnLeaveSceneHandler(Actor me);
        public OnLeaveSceneHandler OnLeaveSceneEvent, OnEnterSceneEvent;

        public bool isMotor { get; set; }

        public bool isEnter { get; set; }

        public bool isLeave { get; set; }

        public bool isHit { get; set; }

        public Microsoft.Xna.Framework.Vector2 ParticlePosition { get { return Position; } set { BodyPosiotion = value; } }

        public Microsoft.Xna.Framework.Vector2 Velocity { get; set; }

        public Microsoft.Xna.Framework.Vector2 Acceleration { get; set; }

        public virtual void Reset()
        {
            isMotor = true;
            isEnter = false;
            isLeave = false;
            isDead = false;
            isHit = false;
            Velocity = Acceleration = Vector2.Zero;
        }

        protected override void UpdateSelf(GameTime gameTime)
        {
            base.UpdateSelf(gameTime);

            Velocity += (Acceleration * gameTime.ElapsedGameTime.Milliseconds / 1000);
            Vector2 v = new Vector2(Velocity.X, -Velocity.Y);
            if (isMotor)
            {
                body.SetLinearVelocity(v);
            }
            if (!inScreen() && !isLeave && isEnter)
            {
                isLeave = true;
                isDead = true;
                isEnter = false;
                if (OnLeaveSceneEvent != null)
                    OnLeaveSceneEvent(this);
            }

            if (inScreen() && !isEnter)
            {
                isEnter = true;
                isLeave = false;
                if (OnEnterSceneEvent != null)
                    OnEnterSceneEvent(this);
            }
           
        }

        public virtual bool inScreen()
        {
            return Position.Y < Constants.Window_Height && Position.Y > 0 && Position.X > 0 && Position.X < Constants.Window_Width;
        }

        public bool isDead { get; set; }
        public float Angle { get; set; }
        public int LifeTime { get; set; }
    }
}
